package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DishServicelmpl implements DishService {
    // 注入DishMapper
    @Autowired
    private DishMapper dishMapper;
    //注入DishFlavorMapper
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    // 注入 SetmealDishMapper
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional//事务注解
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();//创建Dish对象
        BeanUtils.copyProperties(dishDTO, dish);//对象属性拷贝
        //向菜品表插入1条数据
        dishMapper.insert(dish);
        //获取插入后的菜品id
        Long dishId = dish.getId();
        //向口味表插入n条数据
        List<DishFlavor> flavors = dishDTO.getFlavors();//获取 flavors
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));//批量插入菜品ID
            dishFlavorMapper.insertBatch(flavors);//批量插入菜品口味
        }

    }

    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
        //设置分页查询参数
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //执行分页查询
        Page<DishVO> page = dishMapper.page(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断菜品是否在售
        ids.forEach(id -> {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        });
        //判断当前菜品是否和套餐关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if (setmealIds != null && !setmealIds.isEmpty()) {
            //当前菜品有在售套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //批量删除菜品
        dishMapper.deleteBatch(ids);
        //批量删除菜品关联的口味数据
        dishFlavorMapper.deleteByDishId(ids);

    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //1.根据id查询菜品数据
        Dish dish = dishMapper.getById(id);
        //2.根据id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        //3.将数据封装到VO中
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    @Transactional
    public void updateWithFlavors(DishDTO dishDTO) {
        //1.修改菜品数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        //2.修改口味数据
        dishFlavorMapper.deleteByDishId(Collections.singletonList(dishDTO.getId()));//删除当前菜品对应的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();//获取 flavors
        //为口味添加菜品ID
        if (flavors != null && !flavors.isEmpty()) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishDTO.getId()));//批量插入菜品ID
            dishFlavorMapper.insertBatch(flavors);//插入新的口味数据
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        //返回菜品数据
        return dishMapper.list(categoryId);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        //创建一个Dish对象
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.update(dish);
    }

    @Override
    public List<DishVO> listwithFlavor(Dish dish) {
        //获取DishVO对象
        List<Dish> dishList = dishMapper.listByDish(dish);
        //创建视图展示对象
        List<DishVO> dishVOList = new ArrayList<>();
        //遍历dishList
        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            //拷贝数据
            BeanUtils.copyProperties(d, dishVO);
            dishVO.setFlavors(dishFlavorMapper.getByDishId(d.getId()));
            dishVOList.add(dishVO);
        }
        return dishVOList;
    }

    @Override
    public Long getCategoryById(Long id) {
        return dishMapper.getCategoryById(id);
    }
}
